<?php

namespace App\Http\Services;

use App\Http\Model\AccompanySmallRoom\AwardLogs;
use App\Http\Model\Prop;
use App\Http\Model\Room;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\RoomMicro;
use Illuminate\Support\Facades\DB;

/**
 * 陪伴型小屋
 *
 * Created on 2021/10/26 15:11
 * @Author <yuxiang.zhu@asiainnovations.net>
 */
class AccompanySmallRoomServices extends CommonServices
{
    /**
     * 发奖励道具
     *
     * Created at 2021/10/26 15:34
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $roomId
     */
    public function sendAward(int $roomId)
    {
        $propId = $this->randomOneProp();
        if ($propId) {
            $propSystemService = new CommonSystemMsgServices();
            $hostUserId = intval((new RoomHostMicro())->get_user_id($roomId));
            $toUserId = intval((new RoomMicro())->get_user_id($roomId, 0));
            DB::beginTransaction();
            AwardLogs::query()->create(['room_id' => $roomId, 'to_user_id' => $hostUserId, 'award_id' => $propId, 'award_type' => AwardLogs::AWARD_TYPE_OF_PROP]);
            (new Prop())->grantProp($hostUserId, $propId, 0, 1, 55);
            $propSystemService->sendPropSystemMsg($hostUserId, $propId);
            $propId = $this->randomOneProp();//再次随机一个
            if ($toUserId && $propId) {
                AwardLogs::query()->create(['room_id' => $roomId, 'to_user_id' => $toUserId, 'award_id' => $propId, 'award_type' => AwardLogs::AWARD_TYPE_OF_PROP]);
                (new Prop())->grantProp($toUserId, $propId, 0, 1, 55);
                $propSystemService->sendPropSystemMsg($toUserId, $propId);
            }

            DB::commit();
            $data = ['content' => '恭喜你们互相陪伴了' . intval(($this->getMinTime()) / 60) . '分钟 ，神秘礼物已经发放到你们的道具背包啦！',];
            (new CommonRoomMsgServices())->sendRoomMsg($roomId, 30802, $data);
        }
    }

    /**
     * 随机获取一个道具id
     *
     * Created at 2021/10/26 15:30
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return int
     */
    public function randomOneProp(): int
    {
        $props = $this->getAwardProps();
        if (!$props) {
            return 0;
        }

        $rand = rand(0, count($props) - 1);
        return $props[$rand];
    }

    /**
     * 获取奖池(道具)
     *
     * Created at 2021/10/26 15:18
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array
     */
    public function getAwardProps(): array
    {
        $key = 'accompany_room_award_props';
        $configs = $this->getSystemConfig();
        if ($configs && isset($configs[$key]['value']) && $configs[$key]['value']) {
            $props = explode(',', $configs[$key]['value']);
        } else {
            $props = [423, 424, 425, 426, 427];
        }

        if ($props) {
            foreach ($props as &$propId) {
                $propId = intval(trim($propId));
            }
        }

        return $props;
    }

    /**
     * 获取获得陪伴奖励的最少陪伴时间：单位,秒
     *
     * Created at 2021/11/1 19:47
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return int
     */
    public function getMinTime(): int
    {
        $key = 'accompany_room_get_award_min_time';
        $configs = $this->getSystemConfig();
        return intval($configs[$key]['value'] ?? 1800);
    }
}